package cn.com.blueInfo.HaiYuan.business.supplier.service.impl;

import cn.com.blueInfo.HaiYuan.business.supplier.entity.SupplierBase;
import cn.com.blueInfo.HaiYuan.business.supplier.mapper.SupplierBaseMapper;
import cn.com.blueInfo.HaiYuan.business.supplier.service.SupplierBaseService;
import cn.com.blueInfo.utils.result.BootstrapTable;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.JSONUtil;
import cn.com.blueInfo.utils.util.RequestPayload;
import cn.com.blueInfo.utils.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 供应商信息基本信息服务接口具体实现
 * @ClassName: SupplierBaseServiceImpl
 * @author suxch
 * @date 2019年4月24日  上午9:48:03
 */
@Service("SupplierBaseService")
public class SupplierBaseServiceImpl implements SupplierBaseService {

    private static final Logger logger = LoggerFactory.getLogger(SupplierBaseServiceImpl.class);

    @Autowired
    private SupplierBaseMapper baseMapper;

    /**
     * 分页查询供应商信息基本数据具体实现
     * @Title: querySupplierBaseDataByPage
     * @param request
     * @return BootstrapTable
     */
    @Override
    public BootstrapTable querySupplierBaseDataByPage(HttpServletRequest request) {
        BootstrapTable result = new BootstrapTable();
        JSONObject param = RequestPayload.analysis(request);
        logger.info("bootstrapTable请求参数" + param);

        Map<String, Object> paramMap = JSON.parseObject(param.toJSONString());

        try {
            Integer count = baseMapper.querySupplierBaseInfoCount(paramMap);
            List<SupplierBase> supplierBaseList = baseMapper.querySupplierBaseInfo(paramMap);
            result.setTotal(count);
            result.setRows(supplierBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setTotal(0);
            result.setRows(null);
        }
        return result;
    }

    /**
     * 查询供应商基本数据具体实现
     * @Title: querySupplierBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo querySupplierBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        Map<String, Object> paramMap = JSON.parseObject(requestData);

        try {
            List<SupplierBase> supplierBaseList = baseMapper.querySupplierBaseInfo(paramMap);
            result.setResult(true);
            result.setMessage("供应商信息获取成功");
            result.setData(supplierBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setResult(false);
            result.setMessage("供应商信息获取失败");
            result.setError(e.getMessage());
        }
        return result;
    }

    /**
     * 获取最大排序编号具体实现
     * @Title: queryMaxOrderNumber
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryMaxOrderNumber(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        try {
            Integer maxNum = baseMapper.queryMaxOrderNumber();
            if (maxNum == null) maxNum = 0;
            maxNum += 1;
            result.setData(maxNum);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("最大编号获取失败，请联系管理员");
            result.setError(e.getMessage());
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 新增供应商信息基本数据具体实现
     * @Title: addSupplierBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo addSupplierBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            SupplierBase supplier = JSON.parseObject(requestData, SupplierBase.class);
            supplier.setUnid(UUIDUtil.createUUID());
			/*
			Map<String, Object> queryParam = new HashMap<String, Object>();
			queryParam.put("code", supplier.getCode());*/

            try {
                /*Integer isExist = baseMapper.querySupplierBaseInfoCount(queryParam);*/
                Integer isExist = 0;
                if (isExist == 1) {
                    result.setResult(false);
                    result.setMessage("该供应商信息已存在，请修改");
                } else {
                    int add = baseMapper.addSupplierBaseInfo(supplier);
                    if (add == 1) {
                        result.setResult(true);
                        result.setMessage("供应商信息添加成功");
                    }
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setError(e.getMessage());
                result.setMessage("供应商信息增加失败，请联系管理员");
                logger.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常，请联系管理员");
        }
        return result;
    }

    /**
     * 更新供应商信息基本数据具体实现
     * @Title: updateSupplierBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo updateSupplierBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            SupplierBase supplier = JSON.parseObject(requestData, SupplierBase.class);

            try {
                int update = baseMapper.updateSupplierBaseInfoByUnid(supplier);
                if (update == 1) {
                    result.setResult(true);
                    result.setMessage("供应商信息更新成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage("供应商信息更新失败，请联系管理员");
                result.setError(e.getMessage());
                logger.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

    /**
     * 删除供应商基本数据具体实现
     * @Title: deleteSupplierBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo deleteSupplierBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestParam = request.getParameter("requestParam");
        JSONObject param = JSON.parseObject(requestParam);
        String unid = param.getString("unid");
        if (unid == null || "".equals(unid)) {
            result.setResult(false);
            result.setMessage("参数错误");
            return result;
        }

        try {
            int delete = baseMapper.delSupplierBaseInfoByUnid(unid);
            if (delete > 0) {
                result.setResult(true);
                result.setMessage("供应商信息删除成功");
            } else {
                result.setResult(false);
                result.setMessage("供应商信息删除失败");
            }
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("供应商信息删除失败，请联系管理员");
            result.setError(e.getMessage());
            logger.error(e.getMessage());
        }
        return result;
    }

}
